var app = getApp();
Page(Object.assign({},{
    data:{
        inited : false,
        checkedrule : false,
        current : 0,
        show:false,
        num:1,
        total:0,
		siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
    },
    onLoad:function(options){
        var self = this;

        self.setData({
            options : options,
        })

        app.com.auth(function(){
            app.com.http('shopinfo_v1','POST',{op:'buyshopinfo'},0,false,'','',function(res){
                if (!res.data.errno && res.data) {
                    
                    self.setData({
                        inited : true,
                        params : res.data.data.params,
                        shopbag : res.data.data.shopbag ? res.data.data.shopbag : [],
                    });

                }else{
                    app.com.alert(res.data.message,function(){
                        app.com.backPage();
                    });
                }

                wx.setNavigationBarTitle({
                    title: '购买套餐'
                });

            });
        });

    },
    onPullDownRefresh: function () {
        this.onLoad( this.data.options );
        wx.stopPullDownRefresh();
    },
    address(){
        var self = this;
        wx.chooseLocation({
            success : function(res){
                self.setData({
                    lat : res.latitude,
                    lng : res.longitude,
                    address : res.address,
                });
            }
        });
    },
    buy(){
        var price = this.data.shopbag[this.data.current].agentprice;
        this.setData({
            show : !this.data.show,
            total : (price*this.data.num).toFixed(2),
        })
    },
    changenum(e){
        var price = this.data.shopbag[this.data.current].agentprice;

        this.setData({
            num : e.detail,
            total : (price*e.detail).toFixed(2),
        })
    },
    back(){
        wx.navigateBack();
    },
    submit(e){
        var self = this;
        var postdata = {
            op : 'buyshop',
            num : self.data.num,
            bagid : self.data.shopbag[self.data.current].id,
        }
        
        app.com.http('ctyagent','POST',postdata,0,true,'','',function(res){
            if( !res.data.errno && res.data ){
                wx.requestPayment({
                    'appId' : res.data.data.appId,
                    'timeStamp': res.data.data.timeStamp,
                    'nonceStr': res.data.data.nonceStr,
                    'package': res.data.data.package,
                    'signType': 'MD5',
                    'paySign': res.data.data.paySign,
                    'success': function (payres) {
                        if( payres.errMsg == 'requestPayment:ok' ){
                            app.com.alert(res.data.data.mess,function(){
                                wx.navigateBack();
                            });
                            return false;
                        }
                    },
                    'fail': function (payres) {
                        
                    }
                });
            }else{
                app.com.alert(res.data.message);
            }
        });
        
    },
    checkedrule : function(){
        var self = this;
        self.setData({
            checkedrule : !self.data.checkedrule
        })
    },
    bindRegionChange : function(e){
        var self = this;
        self.setData({
            ctaddress : e.detail.value[0] +','+e.detail.value[1] +','+e.detail.value[2]
        })
    },
    bindsortChange(e){
        let key = e.detail.value;
        this.setData({
            sortname : this.data.sort[key].name,
            sortid : this.data.sort[key].id,
        })
    },
    bindcityChange(e){
        let key = e.detail.value;
        this.setData({
            cityname : this.data.city[key].name,
            cityid : this.data.city[key].pid,
        })
    },
    bindtradeChange(e){
        let key = e.detail.value;
        this.setData({
            tradename : this.data.trade[key].name,
            tradeid : this.data.trade[key].id,
        })
    },
    bindchange(e){
        this.setData({
            current : e.detail
        })
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    showimages : function( e ){
        app.com.showimages( e );
    },
    FormIdsubmit : function(e){

    }
}))